//
//  WordDetailView.m
//  EnglishStudyApp
//
//  Created by Nong Trung Nghia on 10/18/12.
//  Copyright (c) 2012 abc. All rights reserved.
//

#import "WordDetailView.h"
#import "ESWord.h"
@implementation WordDetailView
@synthesize word=_word;
@synthesize delegate=_delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        _imageView =[[UIImageView alloc] initWithFrame:CGRectZero];
        [_imageView setUserInteractionEnabled:YES];
        _vnDescriptionLabel=[[UILabel alloc] initWithFrame:CGRectZero];
        [_vnDescriptionLabel setTextAlignment:UITextAlignmentCenter];
        [_vnDescriptionLabel setUserInteractionEnabled:YES];
        
        _enDescriptionLabel=[[UILabel alloc] initWithFrame:CGRectZero];
        [_enDescriptionLabel setTextAlignment:UITextAlignmentCenter];
        [_enDescriptionLabel setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapImage =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageView:)];
        [_imageView addGestureRecognizer:tapImage];
        
        UITapGestureRecognizer *tapVNLabel =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedVNDescriptionLabel:)];
        [_vnDescriptionLabel addGestureRecognizer:tapVNLabel];
        
        UITapGestureRecognizer *tapENLabel =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedENDescriptionLabel:)];
        [_enDescriptionLabel addGestureRecognizer:tapENLabel];
        [self addSubview:_imageView];
        [self addSubview:_vnDescriptionLabel];
        [self addSubview:_enDescriptionLabel];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat height = ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeRight)?([[UIScreen mainScreen] applicationFrame].size.width-44-48):([[UIScreen mainScreen] applicationFrame].size.height-44-48);
    
    [_imageView setImage:[UIImage imageNamed:_word.imageName]];
    [_imageView setFrame:CGRectMake(0, 50, 400, 400)];
    [_imageView setCenter:CGPointMake(self.frame.size.width/2,_imageView.center.y)];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];

//    [_imageView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];

    [_vnDescriptionLabel setText:_word.vnDescription];
    [_vnDescriptionLabel setFrame:CGRectMake(0, height-100, 300, 30)];
    [_vnDescriptionLabel setCenter:CGPointMake(self.frame.size.width/2,_vnDescriptionLabel.center.y)];
    
    
    [_enDescriptionLabel setText:_word.enDescription];
    [_enDescriptionLabel setFrame:CGRectMake(0, height-50, 300, 30)];
    [_enDescriptionLabel setCenter:CGPointMake(self.frame.size.width/2,_enDescriptionLabel.center.y)];

}

-(void)setWord:(ESWord *)aWord{
    _word=aWord;
    [self layoutSubviews];
}

#pragma mark - 
-(IBAction)clickedImageView:(id)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(wordDetailView:didSelectImageView:)]) {
        [_delegate performSelector:@selector(wordDetailView:didSelectImageView:) withObject:self withObject:_imageView];
    }
}

-(IBAction)clickedVNDescriptionLabel:(id)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(wordDetailView:didSelectVNTextLabel:)]) {
        [_delegate performSelector:@selector(wordDetailView:didSelectVNTextLabel:) withObject:self withObject:_vnDescriptionLabel];
    }
}

-(IBAction)clickedENDescriptionLabel:(id)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(wordDetailView:didSelectENTextLabel:)]) {
        [_delegate performSelector:@selector(wordDetailView:didSelectENTextLabel:) withObject:self withObject:_enDescriptionLabel];
    }
}

@end
